package cn.dream.messengerclientsimple;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import cn.dream.ebag.MessengerLib.MGClient;


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    MGClient mgClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mgClient = new MGClient(MainActivity.this) {
            @Override
            public boolean onConnect(String clientPkg) {
                return false;
            }

            @Override
            public void onDisConnect() {
                Log.d(TAG, "onDisConnect: ");
            }

            @Override
            public void onJsonMsg(String message) {
                Log.i(TAG, "onJsonMsg: " + message);
            }

            @Override
            public void onErrorMsg(String msg) {
                Log.e(TAG, "onErrorMsg: msg=" + msg);
            }

            @Override
            public void onCloseMsg() {
                mgClient.closeConnectByExit();
                finish();
            }
        };
        mgClient.setHeartbeatEnable(false);
        findViewById(R.id.connect_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mgClient.isConnected()) {
                    return;
                }
                mgClient.startConnect("cn.dream.messengerserver", "messengerclientsimple");
            }
        });
        findViewById(R.id.json_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mgClient.sendJsonMsg("{action:101}");
            }
        });

        findViewById(R.id.error_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mgClient.sendErrorMsg("{errorTyp:404}");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
        if (mgClient != null) {
            mgClient.closeConnectByExit();
        }
    }
}
